/*
Copyright (c) 2012 the VATSIM Connector project

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef __LIBVATCONN__STRINGIFY_H__
#define __LIBVATCONN__STRINGIFY_H__

#include "libvatconn.h"
#include <array>
#include <string>
#include <sstream>
#include <iomanip>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/array.hpp>

template <class T>
void VconReadColonTerminatedString(T& stream, std::string& string)
{
  char c = stream.get();
  while (c != ':' && c != '\r' && c != '\n' && c != std::char_traits<char>::eof())
  {
    string += c;
    c = stream.get();
  }
  stream.unget();
}

inline std::ostream& operator<< (std::ostream& str, VconSquawk const& sq)
{
  return str << std::right << std::setfill('0') << std::setw(4) << sq.code;
}

inline std::ostream& operator<< (std::ostream& str, VconFreq const& fq)
{
  return str << std::right << std::setfill('0')
    << std::setw(3) << (fq.kHz / 1000) << "." << std::setw(3) << (fq.kHz % 1000);
}

inline std::ostream& operator<< (std::ostream& str, VconWxMsgSubtype const& st)
{
  switch (st)
  {
  case vconWxMETAR:  return str << "METAR";
  case vconWxCLOUDS: return str << "CLOUDS";
  case vconWxWINDS:  return str << "WINDS";
  case vconWxTEMPS:  return str << "TEMPS";
  default:	     return str << "invalid";
  }
}

inline std::istream& operator>> (std::istream& str, VconSquawk& sq)
{
  return str >> std::right >> std::setfill('0') >> std::setw(4) >> sq.code;
}

inline std::istream& operator>> (std::istream& str, VconFreq& fq)
{
  int MHz, kHz;
  str >> std::right >> std::setfill('0');
  str >> std::setw(3) >> MHz;
  str.get(); // drop the decimal point
  str >> std::setw(3) >> kHz;
  fq.kHz = kHz + MHz * 1000;
  return str;
}

inline std::istream& operator>> (std::istream& str, VconWxMsgSubtype& st)
{
  std::string string;
  VconReadColonTerminatedString(str, string);
  if (string == "METAR")	st = vconWxMETAR;
  else if (string == "CLOUDS")	st = vconWxCLOUDS;
  else if (string == "WINDS")	st = vconWxWINDS;
  else if (string == "TEMPS")	st = vconWxTEMPS;
  else				st = vconWxInvalid;
  return str;
}

class VconStringifier
{
public:
  VconStringifier() : m_stream(m_string.data(), m_string.size()) {}

  VconStringifier& operator<< (int x)			  { m_stream << ":" << x; return *this; }
  VconStringifier& operator<< (double x)		  { m_stream << ":" << std::setprecision(11) << x; return *this; }
  VconStringifier& operator<< (bool x)			  { m_stream << ":" << (x ? 1 : 0); return *this; }
  VconStringifier& operator<< (std::string const& x)	  { m_stream << ":" << x; return *this; }
  VconStringifier& operator<< (VconSquawk const& x)	  { m_stream << ":" << x; return *this; }
  VconStringifier& operator<< (VconFreq const& x)	  { m_stream << ":" << x; return *this; }
  VconStringifier& operator<< (VconWxMsgSubtype const& x) { m_stream << ":" << x; return *this; }

  char const* stringify() { m_stream.put(0); return m_string.data(); }

private:
  std::tr1::array<char, vconMaxMsgSize> m_string;
  boost::iostreams::stream<boost::iostreams::array_sink> m_stream;
};

class VconDestringifier
{
public:
  VconDestringifier(char const* str, size_t len, bool doEatColons = true)
  : m_stream(str, len), m_error(false), m_doEatColons(doEatColons)
  {}

  VconDestringifier& operator>> (int& x)	      { if (eatColon()) m_stream >> x; return *this; }
  VconDestringifier& operator>> (double& x)	      { if (eatColon()) m_stream >> x; return *this; }
  VconDestringifier& operator>> (bool& x)	      { if (eatColon()) { int n; m_stream >> n; x = (n != 0); } return *this; }
  VconDestringifier& operator>> (std::string& x)      { if (eatColon()) readString(x); return *this; }
  VconDestringifier& operator>> (VconSquawk& x)	      { if (eatColon()) m_stream >> x; return *this; }
  VconDestringifier& operator>> (VconFreq& x)	      { if (eatColon()) m_stream >> x; return *this; }
  VconDestringifier& operator>> (VconWxMsgSubtype& x) { if (eatColon()) m_stream >> x; return *this; }

  bool error() const { return m_error || m_stream.fail(); }

private:
  boost::iostreams::stream<boost::iostreams::array_source> m_stream;
  bool m_error;
  bool const m_doEatColons;

  bool eatColon()
  {
    if (m_stream.get() == ':') return true; else return !(m_error = true);
  }

  void readString(std::string& str)
  {
    if (m_doEatColons)
      VconReadColonTerminatedString(m_stream, str);
    else
    {
      std::stringbuf buf;
      m_stream.get(buf, '\r');
      str = buf.str();
    }
  }
};

#endif //__LIBVATCONN__STRINGIFY_H__